package cn.jbit.supermarker.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.jbit.supermarker.dao.BaseDao;
import cn.jbit.supermarker.dao.ProviderDao;
import cn.jbit.supermarker.entity.Provider;
import cn.jbit.supermarker.util.Page;

import sun.security.timestamp.TSRequest;

public class ProviderDaoImpl extends BaseDao implements ProviderDao {

	public static void main(String[] args) {
		ProviderDaoImpl providerDaoImpl = new ProviderDaoImpl();
		Provider pro = new Provider();
		// 添加
		// pro.setPro_name("北京大兴区");
		// pro.setPro_desc("是个食品加工厂");
		// pro.setPro_contact("李四");
		// pro.setPro_address("大兴开发区");
		// pro.setPro_fax("9483223");
		// boolean flag=providerDaoImpl.addProvider(pro);
		// 修改信息、
		pro.setPro_id(4);
		pro.setPro_contact("刘能");
		pro.setPro_name("北京朝阳区");
		boolean flag = providerDaoImpl.updateProviderl(pro);
		// 删除信息
		// pro.setPro_id(2);
		// boolean flag=providerDaoImpl.deleteProvider(pro);
		// 查询信息
		List<Provider> proList = providerDaoImpl.getProviderList();
		System.out.println("id" + "\t" + "供应商名称" + "\t" + "联系人");
		for (Provider provider : proList) {
			System.out.println(provider.getPro_id() + "\t"
					+ provider.getPro_name() + "\t\t"
					+ provider.getPro_contact());
		}
		if (flag) {
			System.out.println("成功");
		} else {
			System.out.println("失败");
		}
	}

	// 获得供应商列表信息
	public List<Provider> getProviderList() {
		// TODO Auto-generated method stub
		List<Provider> proList = new ArrayList<Provider>();
		try {
			String sql = "SELECT *FROM smbms_provider";
			Object[] params = {};
			rs = this.executeSQL(sql, params);
			while (rs.next()) {
				Provider pro = new Provider();
				pro.setPro_id(rs.getInt("pro_id"));
				pro.setPro_name(rs.getString("pro_name"));
				pro.setPro_desc(rs.getString("pro_desc"));
				pro.setPro_contact(rs.getString("pro_contact"));
				pro.setPro_phone(rs.getString("pro_phone"));
				pro.setPro_address(rs.getString("pro_address"));
				pro.setPro_fax(rs.getString("pro_fax"));
				proList.add(pro);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			this.closeResource();
		}
		return proList;
	}

	// 添加供应商信息
	public boolean addProvider(Provider provider) {
		try {
			StringBuffer sb = new StringBuffer();
			sb.append("INSERT INTO smbms_provider ");
			sb.append("(pro_name,pro_desc,pro_contact,pro_phone,pro_address,pro_fax,op_time)    ");
			sb.append("values(?,?,?,?,?,?,Now())");
			Object[] params = { provider.getPro_name(), provider.getPro_desc(),
					provider.getPro_contact(), provider.getPro_phone(),
					provider.getPro_address(), provider.getPro_fax() };
			// int result=this.ExecuteUpdate(sb.toString(), params);
			int result = this.ExecuteUpdate(sb.toString(), params);
			if (result > 0) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			this.closeResource();
		}
		return true;
	}

	// 修改供应商信息
	public boolean updateProviderl(Provider provider) {
		try {
			StringBuffer sb = new StringBuffer();
			sb.append("update smbms_provider set pro_name=?,pro_desc=?,pro_contact=?,pro_phone=?,pro_address=?,pro_fax=?    ");
			sb.append("   where pro_id=?");
			Object[] params = { provider.getPro_name(), provider.getPro_desc(),
					provider.getPro_contact(), provider.getPro_phone(),
					provider.getPro_address(), provider.getPro_fax(),
					provider.getPro_id() };
			int result = this.ExecuteUpdate(sb.toString(), params);
			if (result > 0) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			this.closeResource();
		}
		return false;
	}

	// 删除供应商信息
	public boolean deleteProvider(int id) {
		try {

			StringBuffer sb = new StringBuffer();
			sb.append("delete from smbms_provider where pro_id=?");
			Object[] params = { id };
			int result = this.ExecuteUpdate(sb.toString(), params);
			if (result > 0) {
				return true;
			} else {
				return false;

			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			this.closeResource();
		}
		return true;
	}

	public Provider getProById(int id) {
		Provider pro = null;
		try {
			String sql = "select * from smbms_provider where pro_id=" + id;
			Object[] params = {};
			rs = this.executeSQL(sql, params);
			if (rs.next()) {
				pro = new Provider();
				pro.setPro_id(id);
				pro.setPro_name(rs.getString("pro_name"));
				pro.setPro_desc(rs.getString("pro_desc"));
				pro.setPro_contact(rs.getString("pro_contact"));
				pro.setPro_phone(rs.getString("pro_phone"));
				pro.setPro_address(rs.getString("pro_address"));
				pro.setPro_fax(rs.getString("pro_fax"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			this.closeResource();
		}
		return pro;
	}

	// 分页显示供应商列表信息
	public List<Provider> getProPageList(int pageNo, int pageSize,
			String ProName) {
		List<Provider> proList = new ArrayList<Provider>();
		Provider provider = null;
		Page page = new Page();
		page.setCurrPageNo(pageNo);
		page.setPageSize(pageSize);
		try {
			if (ProName != null && !"".equals(ProName)) {
				String sql = "select * from smbms_provider where pro_name like ? order by pro_id asc limit ?,?";
				Object[] params = { "%" + ProName + "%", page.getStartRow(), pageSize };
				rs = this.executeSQL(sql, params);
			} else {
				String sql = "select *from smbms_provider order by pro_id asc limit ?,?";
				Object[] params = { page.getStartRow(), pageSize };
				rs = this.executeSQL(sql, params);
			}
			while (rs.next()) {
				provider = new Provider();
				provider.setPro_id(rs.getInt("pro_id"));
				provider.setOp_time(rs.getDate("op_time"));
				provider.setPro_address(rs.getString("pro_address"));
				provider.setPro_contact(rs.getString("pro_contact"));
				provider.setPro_desc(rs.getString("pro_desc"));
				provider.setPro_fax(rs.getString("pro_fax"));
				provider.setPro_name(rs.getString("pro_name"));
				provider.setPro_phone(rs.getString("pro_phone"));
				proList.add(provider);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.closeResource();
		}
		return proList;
	}

	// 获取总记录数
	public int getTotalCount(String ProName) {
		int totalCount = 0;
		// List<Object>
		try {
			if (ProName != null && !"".equals(ProName)) {
				String sql = "select count(*) from smbms_provider where pro_name like ?";
				Object[] params = { "%" + ProName + "%" };
				rs = this.executeSQL(sql, params);
			} else {
				String sql = "select count(*) from smbms_provider";
				Object[] params = {};
				rs = this.executeSQL(sql, params);
			}
			while (rs.next()) {
				totalCount = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.closeResource();
		}
		return totalCount;
	}

}
